using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using NHibernate.Mapping.Attributes;
using Lib;

namespace Persistance.Model
{

    /**
     * Situation Specification
     * 
     * SituationID (int) --> Uniquely Identifies the Situation
     * ListOfConcerns (string) --> Stores the list of Concerns that Author entered
     * CreatedOn (datetime) --> The Date the Situation was created On
     * CreatedBy (string) --> Uniquely identifies the Author that first created the Situation
     * LastUpdated (datetime) --> The Date the Situation was last updated
     * LastUpdatedBy (string) --> Uniquely identifies the Author that last editted the Situation
     * 
     * */

    [Serializable]
    [Class]
    public class Situation
    {

        private int _situationID;
        //NHIBER: Setts the primary key to ID
        [Id(Name = "SituationID")]
        [Generator(1, Class = "native")]
        public virtual int SituationID
        {
            get { return _situationID; }
            set { _situationID = value; }
        }

        private IList<Problem> _problems = new List<Problem>();
        [Bag(1, Cascade = CascadeStyle.AllDeleteOrphan)]
        [Key(2, Column = "SituationID", ForeignKey = "FK_Situation")]
        [OneToMany(3, ClassType = typeof(Problem))]
        public virtual IList<Problem> Problems
        {
            get { return _problems; }
            set { _problems = value; }
        }

        private Users _user = null;
        [ManyToOne(Column = "AuthorID", ForeignKey = "FK_SituationAuthor")]
        public virtual Users Author
        {
            get { return _user; }
            set { _user = value; }
        }

        private Users _lastEdittedByUser = null;
        [ManyToOne(Column = "LastEittedBy", ForeignKey = "FK_LastEdittedByAuthor")]
        public virtual Users LastEdittedBy
        {
            get { return _lastEdittedByUser; }
            set { _lastEdittedByUser = value; }
        }

        /// <summary>
        /// Gets or Sets the Date that the PA was originally created on
        /// </summary>
        private DateTime _createdOn = new DateTime(1008, 1, 1);
        [Property(Type = ("DateTime"))]
        public virtual DateTime CreatedOn
        {
            get { return _createdOn; }
            set { _createdOn = value; }
        }

        private DateTime _edittedOn = new DateTime(2008, 1, 1);
        [Property(Type = ("DateTime"))]
        public virtual DateTime EdittedOn
        {
            get { return _edittedOn; }
            set { _edittedOn = value; }
        }


        public virtual void Create()
        {
            ISession sess = Persistor.CurrentSession;
            try
            {
                sess.Transaction.Begin();
                sess.Save(this);
                sess.Transaction.Commit();
                sess.Flush();
            }
            catch (Exception e)
            {
                sess.Transaction.Rollback();
                throw new Exception("akljsghp", e);
            }
        }
    }
}
